package viroyal.com.base.fragment;


import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.mingle.widget.LoadingView;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;

import viroyal.com.base.R;
import viroyal.com.base.model.Announced;
import viroyal.com.base.widget.JavascriptInterface;
import viroyal.com.base.widget.X5WebView;


/**
 *
 * @author gaochao
 */
public class MultiplexFragment extends BaseFragment {
  private String TAG = "MultiplexFragment";
  private View view;
  private String url;
  private X5WebView wvTask;
  private ProgressBar progressBar1;
  private String title;

  public MultiplexFragment() {
    // Required empty public constructor
  }
  /**
   *
   * @return fragment实例
   */
  public static BaseFragment newInstance(String url, String title) {
    MultiplexFragment myMultiplexFragment = new MultiplexFragment();
    Bundle bundle = new Bundle();
    bundle.putString("FragmentUrl", url);
    bundle.putString("FragmentTitle", title);
    myMultiplexFragment.setArguments(bundle);
    return myMultiplexFragment;
  }

//  @Override
//  public View onCreateView(LayoutInflater inflater, ViewGroup container,
//                           Bundle savedInstanceState) {
//    view = inflater.inflate(R.layout.fragment_one, container, false);
//    initview(view);
//    return view;
//  }

  @Override
  protected int setContentView() {
    return  R.layout.fragment_service;
  }

  @Override
  protected void lazyLoad() {
    Bundle arguments = getArguments();
    if (arguments != null) {
      url = arguments.getString("FragmentUrl");
      title = arguments.getString("FragmentTitle");
      Log.i(TAG, "updateArguments: FragmentUrl"+ url);
    }

    TextView tvTitle = findViewById(R.id.tv_title);
    LinearLayout llNotice = findViewById(R.id.ll_dialog_notice);
    ImageView ivCloseNotice = findViewById(R.id.iv_close_notice);
    ivCloseNotice.setOnClickListener(view -> {
      getActivity().finish();
      getActivity().overridePendingTransition(R.anim.slide_left_in,
          R.anim.slide_right_out);
    });
    LoadingView loadView = findViewById(R.id.loadView);
    wvTask = findViewById(R.id.web_view_notice_value);
    progressBar1 = findViewById(R.id.progressBar1);
    tvTitle.setText(title);

    if (!TextUtils.isEmpty(url)) {
      wvTask.loadUrl(url);
    } else {
//      wvTask.loadData(value, "text/html; charset=UTF-8", "UTF-8");
    }
  //监听网页的加载进度
    wvTask.setWebChromeClient(new WebChromeClient(){
    @Override
    public void onProgressChanged(WebView webView, int i) {
      super.onProgressChanged(webView, i);
      if (i < 100) {
        progressBar1.setVisibility(View.VISIBLE);
        llNotice.setVisibility(View.GONE);
        loadView.setVisibility(View.VISIBLE);
        progressBar1.setProgress(i);
      } else {
        progressBar1.setVisibility(View.INVISIBLE);
        loadView.setVisibility(View.GONE);
        llNotice.setVisibility(View.VISIBLE);
      }
    }
  });
    wvTask.addJavascriptInterface(new JavascriptInterface(), "AndroidWebView");
//    LinearLayout ll_one = (LinearLayout) findViewById(R.id.ll_one);
//    loadUrl(ll_one, url);

  }


  //销毁 放置内存泄漏
  @Override
  public void onDestroy() {
    if (this.wvTask != null) {
      wvTask.destroy();
    }
    super.onDestroy();
  }

}
